#include "LinkStack.h"

struct Queue
{
    LStack in; // 进数据的栈
    LStack out; // 出数据的栈
};
typedef struct Queue queue;

void InitQueue(queue *q)
{
    InitLStack(&q->in);
    InitLStack(&q->out);
}

void QPush(queue *q, ElementType element)
{
    while (IsStackEmpty(&q->out) == false)
    {
        // 把出的栈的数据倒回左边
        SPush(&q->in, *GetTop(&q->out));
        SPop(&q->out);
    }
    SPush(&q->in, element);
}

void QPop(queue *q)
{
    if (IsStackEmpty(&q->out) == true)
    {
        while (IsStackEmpty(&q->in) == false)
        {
            SPush(&q->out, *GetTop(&q->in));
            SPop(&q->in);
        }
    }
    SPop(&q->out);
}

ElementType* GetFront(queue *q)
{
    if (IsStackEmpty(&q->out) == true)
    {
        while (IsStackEmpty(&q->in) == false)
        {
            SPush(&q->out, *GetTop(&q->in));
            SPop(&q->in);
        }
    }
    return GetTop(&q->out);
}

bool IsQueueEmpty(queue *q)
{
    return IsStackEmpty(&q->in) && IsStackEmpty(&q->out);
}

int main()
{
    queue q;
    InitQueue(&q);
    QPush(&q, 10);
    QPush(&q, 11);
    QPush(&q, 12);
    while (IsQueueEmpty(&q) == false)
    {
        printf("%d ", (int)*GetFront(&q));
        QPop(&q);
    }
    printf("\n");
    return 0;
}